//
//  WebViewController.m
//  小团购
//
//  Created by 樊荣海 on 15/2/4.
//  Copyright (c) 2015年 樊荣海. All rights reserved.
//

#import "WebViewController.h"

@interface WebViewController () <UIWebViewDelegate>

@end

@implementation WebViewController {


    UIWebView *_webView;
    UIView *_cover;
}


- (void)loadView {
    
    _webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    _webView.delegate = self;
    _webView.backgroundColor = globalBgColor;
    _webView.scrollView.backgroundColor = globalBgColor;
    self.view = _webView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"id : %@", _dealModel.deal_id);
    
    NSString *ID = [_dealModel.deal_id substringFromIndex:[_dealModel.deal_id rangeOfString:@"-"].location + 1];
    NSString *url = [NSString stringWithFormat:@"http://m.dianping.com/tuan/deal/moreinfo/%@", ID];
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
    
     NSLog(@"url: %@", url);
    
    
    // 2.添加遮盖
    _cover = [[UIView alloc] init];
    _cover.frame = _webView.bounds;
    _cover.backgroundColor = globalBgColor;
    _cover.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [_webView addSubview:_cover];
    
    // 3.添加圈圈
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    indicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin |  UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
    CGFloat x = _cover.frame.size.width * 0.5;
    CGFloat y = _cover.frame.size.height * 0.5;
    indicator.center = CGPointMake(x, y);
    [_cover addSubview:indicator];
    [indicator startAnimating];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    return YES;
}


#warning mark 此处网页待处理
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    
    // 0.删除scrollView顶部和底部灰色的view
    for (UIView *view in webView.scrollView.subviews) {
        if ([view isKindOfClass:[UIImageView class]]) {
            [view removeFromSuperview];
        }
    }
    
    CGFloat height = 70;
    webView.scrollView.contentInset = UIEdgeInsetsMake(height, 0, 0, 0);//增加顶部滚动区域
    webView.scrollView.contentOffset = CGPointMake(0, -height);//向下滚70
    
    
    // 1.拼接脚本
    NSMutableString *script = [NSMutableString string];
    // 1.1.取出body
    [script appendString:@"var body = document.body;"];
    // 1.2.取出section
    [script appendString:@"var section = (document.getElementsByTagName('body')[5]).innerHTML;"];
    // 1.3.清空body
    [script appendString:@"body.innerHTML = '';"];
    // 1.4.添加section到body
    [script appendString:@"body.appendChild(section);"];
    
    // 2.执行脚本
    [_webView stringByEvaluatingJavaScriptFromString:script];
    
    NSLog(@"script: %@", script);
    
    
    // 3.移除遮盖
    [_cover removeFromSuperview];
    _cover = nil;

}




@end
